package com.ruoyi.basebill.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson2.JSON;
import com.qimencloud.api.DefaultQimenCloudClient;
import com.qimencloud.api.scene5uc0mzv618.request.WdtAftersaleReturnCollectQueryRequest;
import com.qimencloud.api.scene5uc0mzv618.request.WdtOnsaleCollectQueryRequest;
import com.qimencloud.api.scene5uc0mzv618.response.WdtAftersaleReturnCollectQueryResponse;
import com.qimencloud.api.scene5uc0mzv618.response.WdtOnsaleCollectQueryResponse;
import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.*;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.webServiceUtil.WebServiceHelper;
import com.taobao.api.ApiException;
import org.apache.axis.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.format.DateTimeFormatter;
import java.util.List;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.util.Date;

/**
 * 推送EAS单据定时任务
 */
@Component("PushEASBillDataTask")
public class PushEASBillDataTask {
    private final static Logger logger = LoggerFactory.getLogger(PushEASBillDataTask.class);

    @Autowired
    private IOmsSumpurinwarehsbillService sumPurinwarehsbillService;    //汇总采购入库单serverice层

    @Autowired
    private IOmsSaleissuebillService saleissuebillService;    //销售出库单serverice层

    @Autowired
    private IOmsSumsaleissuebillService sumSaleissuebillService;    //销售出库单serverice层

    @Autowired
    private IOmsOtherissuebillService otherissuebillService;    //其他出库单serverice层

    @Autowired
    private IOmsSumstocktransferbillService sumStockTransferbillService;    //库存调拨单serverice层

    @Autowired
    private IOmsTransferbillService omsTransferbillService;    //调拨单serverice层

    @Autowired
    private IOmsDeliverbillService omsDeliverbillService;    //发货确认单serverice层

    @Autowired
    private IOmsReceiptbillService omsReceiptbillService;    //收货确认单serverice层

    @Autowired
    private IOmsSalereturnbillService omsSalereturnbillService;    //销售退货单serverice层

    @Autowired
    private ISysDictDataService sysDictDataService;    //键值对key值serverice层

    @Autowired
    private IWfzWebserviceService wfzWebserviceService; //调用金蝶webservice接口封装层
    //系统参数服务层接口
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 定时推送汇总库存调拨单
     */
    public synchronized void pushSumStockTransferBill(){
        logger.info("------------------------开始查询没推送金蝶的汇总库存调拨单");
        System.out.println("------------------------开始查询没推送金蝶的汇总库存调拨单");
        List<OmsSumstocktransferbill> listInfo=  sumStockTransferbillService.selectPushEASSumStockTransferbillList(1);
        //循环找出对应没推送过EAS的汇总库存调拨单
        for(int i = 0;i<listInfo.size();i++){
            OmsSumstocktransferbill pushEasBill = listInfo.get(i);
            JSONObject requestDataJSON = JSONObject.parseObject(JSONObject.toJSONString(pushEasBill,SerializerFeature.WriteDateUseDateFormat));
            requestDataJSON.put("detail",requestDataJSON.getJSONArray("omsSumstocktransferbillentryList"));
            requestDataJSON.remove("omsSumstocktransferbillentryList");
            requestDataJSON.put("sourceSystem","结算中心");//源系统标识
            //修改分录信息
            JSONArray entryJson = requestDataJSON.getJSONArray("detail");
            for(int j = 0;j<entryJson.size();j++) {
                entryJson.getJSONObject(j).put("entNo", j + 1);
            }
            JSONObject pushJSON = new JSONObject(); //组装默认推送值
            JSONObject serverJSON = new JSONObject(); //组装对应单据类型
            serverJSON.put("name","receiveStockTransferBill");//receiveStockTransferBill金蝶汇总库存调拨单标识
            pushJSON.put("service",serverJSON);
            pushJSON.put("requestData",requestDataJSON);
            String callJSON = pushJSON.toJSONString();  //推送金蝶单据参数信息
            logger.info("------------------------推送金蝶汇总库存调拨单入参case"+callJSON);
            System.out.println("------------------------推送金蝶汇总库存调拨单入参case"+callJSON);
            String easrs = toPushEasWebservice(callJSON,"OMS-EAS001");
            //需要修改的汇总库存调拨单
            OmsSumstocktransferbill updateBill = new OmsSumstocktransferbill();
            updateBill.setId(pushEasBill.getId());
            updateBill.setTranstime(new Date());
            if(easrs == null){
                updateBill.setIstrans("1");
                updateBill.setReason("推送金蝶成功");
            }else{
                if (easrs.contains("已存在不允许重复推送") || easrs.contains("请检查入参物料是否在金蝶配置了不需要实际出入库")){
                    updateBill.setIstrans("1");
                    updateBill.setReason(easrs);
                }else{
                    updateBill.setIstrans("0");
                    updateBill.setReason("推送金蝶失败，失败信息"+easrs);
                }

            }
            sumStockTransferbillService.updateOmsSumstocktransferbill(updateBill);
            logger.info("------------------------推送金蝶汇总库存调拨单入参case"+easrs);
            System.out.println("------------------------推送金蝶汇总库存调拨单入参end"+easrs);
        }
    }

    /**
     * 定时推送汇总采购入库单
     */
    public synchronized void pushSumPurInWarehsBill(){
        logger.info("------------------------开始查询没推送金蝶的汇总采购入库单");
        System.out.println("------------------------开始查询没推送金蝶的汇总采购入库单");
        List<OmsSumpurinwarehsbill> listInfo=  sumPurinwarehsbillService.selectPushEASSumPurinwarehsbillList(1);
        //循环找出对应没推送过EAS的汇总采购入库单
        for(int i = 0;i<listInfo.size();i++){
            OmsSumpurinwarehsbill pushEasBill = listInfo.get(i);
            JSONObject requestDataJSON = JSONObject.parseObject(JSONObject.toJSONString(pushEasBill,SerializerFeature.WriteDateUseDateFormat));
            requestDataJSON.put("detail",requestDataJSON.getJSONArray("omsSumpurinwarehsbillentryList"));
            requestDataJSON.remove("omsSumpurinwarehsbillentryList");
            requestDataJSON.put("sourceSystem","结算中心");//源系统标识
            //修改分录信息
            JSONArray entryJson = requestDataJSON.getJSONArray("detail");
            for(int j = 0;j<entryJson.size();j++) {
                entryJson.getJSONObject(j).put("entNo", j + 1);
            }
            JSONObject pushJSON = new JSONObject(); //组装默认推送值
            JSONObject serverJSON = new JSONObject(); //组装对应单据类型
            serverJSON.put("name","receivePurInWarehsBill");//receivePurInWarehsBill金蝶汇总采购入库单标识
            pushJSON.put("service",serverJSON);
            pushJSON.put("requestData",requestDataJSON);
            String callJSON = pushJSON.toJSONString();  //推送金蝶单据参数信息
            logger.info("------------------------推送金蝶汇总采购入库单入参case"+callJSON);
            System.out.println("------------------------推送金蝶汇总采购入库单入参case"+callJSON);
            String easrs = toPushEasWebservice(callJSON,"OMS-EAS001");
            //需要修改的汇总采购入库单
            OmsSumpurinwarehsbill updateBill = new OmsSumpurinwarehsbill();
            updateBill.setId(pushEasBill.getId());
            updateBill.setTranstime(new Date());
            if(easrs == null){
                updateBill.setIstrans("1");
                updateBill.setReason("推送金蝶成功");
            }else{
                if (easrs.contains("已存在不允许重复推送") || easrs.contains("请检查入参物料是否在金蝶配置了不需要实际出入库")){
                    updateBill.setIstrans("1");
                    updateBill.setReason(easrs);
                }else {
                    updateBill.setIstrans("0");
                    updateBill.setReason("推送金蝶失败，失败信息" + easrs);
                }
            }
            sumPurinwarehsbillService.updateOmsSumpurinwarehsbill(updateBill);
            logger.info("------------------------推送金蝶汇总采购入库单入参case"+easrs);
            System.out.println("------------------------推送金蝶汇总采购入库单入参end"+easrs);
        }
    }

    /**
     * 定时推送其他出库单
     */
    public synchronized void pushOtherIssueBill(){
        logger.info("------------------------开始查询没推送金蝶的其他出库单");
        System.out.println("------------------------开始查询没推送金蝶的其他出库单");
        List<OmsOtherissuebill> listInfo=  otherissuebillService.selectEasOtherissuebillServiceList(1);
        //循环找出对应没推送过EAS的其他出库库单
        for(int i = 0;i<listInfo.size();i++){
            OmsOtherissuebill pushEasBill = listInfo.get(i);
            JSONObject requestDataJSON = JSONObject.parseObject(JSONObject.toJSONString(pushEasBill,SerializerFeature.WriteDateUseDateFormat));
            requestDataJSON.put("detail",requestDataJSON.getJSONArray("omsOtherissuebillentryList"));
            requestDataJSON.remove("omsOtherissuebillentryList");//分录明细
            requestDataJSON.put("code",requestDataJSON.getString("billnumber"));
            requestDataJSON.remove("billnumber");   //单据编码
            requestDataJSON.put("transactionType",requestDataJSON.getString("transactiontypenumber"));
            requestDataJSON.remove("transactiontypenumber");   //事务类型编码
//            requestDataJSON.put("remark",requestDataJSON.getString("transactionType"));
//            requestDataJSON.remove("transactionType");   //备注
            requestDataJSON.put("sourceSystem","结算中心");//源系统标识
            //修改分录信息
            JSONArray entryJson = requestDataJSON.getJSONArray("detail");
            for(int j = 0;j<entryJson.size();j++){
                entryJson.getJSONObject(j).put("entNo",j+1);
                entryJson.getJSONObject(j).put("skuCode",entryJson.getJSONObject(j).getString("materialnumber"));
                entryJson.getJSONObject(j).remove("materialnumber");//物料编码
                entryJson.getJSONObject(j).put("baseQty",entryJson.getJSONObject(j).getBigDecimal("baseqty"));
                entryJson.getJSONObject(j).remove("baseqty");//基本数量
                entryJson.getJSONObject(j).put("facilityCode",entryJson.getJSONObject(j).getString("warehousenumber"));
                entryJson.getJSONObject(j).remove("warehousenumber");//仓库编码
            }
            JSONObject pushJSON = new JSONObject(); //组装默认推送值
            JSONObject serverJSON = new JSONObject(); //组装对应单据类型
            serverJSON.put("name","receiveOtherIssueBill");//receiveOtherIssueBill金蝶其他出库单标识
            pushJSON.put("service",serverJSON);
            pushJSON.put("requestData",requestDataJSON);
            String callJSON = pushJSON.toJSONString();  //推送金蝶单据参数信息
            logger.info("------------------------推送金蝶其他出库单入参case"+callJSON);
            System.out.println("------------------------推送金蝶其他出库单入参case"+callJSON);
            String easrs = toPushEasWebservice(callJSON,"OMS-EAS001");
            //需要修改的其他出库单
            OmsOtherissuebill updateBill = new OmsOtherissuebill();
            updateBill.setId(pushEasBill.getId());
            updateBill.setTranstime(new Date());
            if(easrs == null){
                updateBill.setIstrans("1");
                updateBill.setReason("推送金蝶成功");
            }else{
                if (easrs.contains("已存在不允许重复推送") || easrs.contains("请检查入参物料是否在金蝶配置了不需要实际出入库")){
                    updateBill.setIstrans("1");
                    updateBill.setReason(easrs);
                }else {
                    updateBill.setIstrans("0");
                    updateBill.setReason("推送金蝶失败，失败信息" + easrs);
                }
            }
            otherissuebillService.updateOmsOtherissuebill(updateBill);
            logger.info("------------------------推送金蝶其他出库单入参case"+easrs);
            System.out.println("------------------------推送金蝶其他出库单入参end"+easrs);
        }
    }

    /**
     * 定时推送销售出库单
     */
    public synchronized void pushSaleIssueBill(){
        logger.info("------------------------开始查询没推送金蝶的销售出库单");
        System.out.println("------------------------开始查询没推送金蝶的销售出库单");
        List<OmsSaleissuebill> listInfo=  saleissuebillService.selectPushEASSaleissuebillList(1);
        //循环找出对应没推送过EAS的销售出库单
        for(int i = 0;i<listInfo.size();i++){
            OmsSaleissuebill pushEasBill = listInfo.get(i);
            String srcBillType = pushEasBill.getSrcbilltype();//来源单据类型（调拨单，收货确认，发货确认，销售退货）
            String srcBillNo = pushEasBill.getSrcbillnumber();//来源单据编码
            String sourceNo = "";   //外部系统来源单号比如（KA，早餐，OMS）等系统的单号
            String shipmentHeaderCode = "";//出库单号来源是收货确认单存在
            String sourceType = "";//外部系统来源类型比如（KA，早餐，OMS）
            if(!StringUtils.isEmpty(srcBillType)){
                if(srcBillType.contains("调拨单")){
                    OmsTransferbill omsTransferbill = new OmsTransferbill();
                    omsTransferbill.setCode(srcBillNo);
                    List<OmsTransferbill> listSourceInfo = omsTransferbillService.selectOmsTransferbillList(omsTransferbill);
                    for(int j = 0;j<listSourceInfo.size();j++){
                        OmsTransferbill sourceBill = listSourceInfo.get(j);
                        sourceNo = sourceBill.getSourceordercode();
                        sourceType = sourceBill.getSourceplatformcode();
                    }
                }else if(srcBillType.contains("收货确认")){
                    OmsReceiptbill omsReceiptbill = new OmsReceiptbill();
                    omsReceiptbill.setCode(srcBillNo);
                    List<OmsReceiptbill> listSourceInfo = omsReceiptbillService.selectOmsReceiptbillList(omsReceiptbill);
                    for(int j = 0;j<listSourceInfo.size();j++){
                        OmsReceiptbill sourceBill = listSourceInfo.get(j);
                        sourceNo = sourceBill.getSourceordercode();
                        sourceType = sourceBill.getSourceplatformcode();
                    }
                    shipmentHeaderCode = omsReceiptbill.getShipmentheadercode();
                }else if(srcBillType.contains("发货确认")){
                    OmsDeliverbill omsDeliverbill = new OmsDeliverbill();
                    omsDeliverbill.setCode(srcBillNo);
                    List<OmsDeliverbill> listSourceInfo = omsDeliverbillService.selectOmsDeliverbillList(omsDeliverbill);
                    for(int j = 0;j<listSourceInfo.size();j++){
                        OmsDeliverbill sourceBill = listSourceInfo.get(j);
                        sourceNo = sourceBill.getSourceordercode();
                        sourceType = sourceBill.getSourceplatformcode();
                    }
                }else if(srcBillType.contains("销售退货")){
                    OmsSalereturnbill omsSalereturnbill = new OmsSalereturnbill();
                    omsSalereturnbill.setCode(srcBillNo);
                    List<OmsSalereturnbill> listSourceInfo = omsSalereturnbillService.selectOmsSalereturnbillList(omsSalereturnbill);
                    for(int j = 0;j<listSourceInfo.size();j++){
                        OmsSalereturnbill sourceBill = listSourceInfo.get(j);
                        sourceNo = sourceBill.getSourceordercode();
                        sourceType = sourceBill.getSourceplatformcode();
                    }
                }
            }
//            if(!StringUtils.isEmpty(sourceType)){
//                sourceType = sysDictDataService.selectDictLabel("srcup_type",sourceType);
//            }
            //组装入参JSON
            JSONObject requestDataJSON = JSONObject.parseObject(JSONObject.toJSONString(pushEasBill,SerializerFeature.WriteDateUseDateFormat));
            requestDataJSON.put("detail",requestDataJSON.getJSONArray("omsSaleissuebillentryList"));
            requestDataJSON.remove("omsSaleissuebillentryList");
            requestDataJSON.put("sourceSystem","结算中心");//源系统标识
            requestDataJSON.put("sourceplatformcode",sourceType);//来源平台
            requestDataJSON.put("shipmentheadercode",shipmentHeaderCode);//出库单号
            requestDataJSON.put("sourceordercode",sourceNo);//来源单号
            //修改分录信息
            JSONArray entryJson = requestDataJSON.getJSONArray("detail");
            for(int j = 0;j<entryJson.size();j++) {
                entryJson.getJSONObject(j).put("entNo", j + 1);
            }
            JSONObject pushJSON = new JSONObject(); //组装默认推送值
            JSONObject serverJSON = new JSONObject(); //组装对应单据类型
            serverJSON.put("name","receiveSaleIssueBill");//receiveSaleIssueBill金蝶销售出库单标识
            pushJSON.put("service",serverJSON);
            pushJSON.put("requestData",requestDataJSON);
            String callJSON = pushJSON.toJSONString();  //推送金蝶单据参数信息
            logger.info("------------------------推送金蝶销售出库单入参case"+callJSON);
            System.out.println("------------------------推送金蝶销售出库单入参case"+callJSON);
            String easrs = toPushEasWebservice(callJSON,"OMS-EAS001");
            //需要修改的销售出库单
            OmsSaleissuebill updateBill = new OmsSaleissuebill();
            updateBill.setId(pushEasBill.getId());
            updateBill.setTranstime(new Date());
            if(easrs == null){
                updateBill.setIstrans("1");
                updateBill.setReason("推送金蝶成功");
            }else{
                if (easrs.contains("已存在不允许重复推送") || easrs.contains("请检查入参物料是否在金蝶配置了不需要实际出入库")){
                    updateBill.setIstrans("1");
                    updateBill.setReason(easrs);
                }else {
                    updateBill.setIstrans("0");
                    updateBill.setReason("推送金蝶失败，失败信息"+easrs);
                }
            }
            saleissuebillService.updateOmsSaleissuebill(updateBill);
            logger.info("------------------------推送金蝶销售出库单入参case"+easrs);
            System.out.println("------------------------推送金蝶销售出库单入参end"+easrs);
        }
    }

    /**
     * 定时推送汇总销售出库单
     */
    public synchronized void pushSumSaleIssueBill(){
        logger.info("------------------------开始查询没推送金蝶的汇总销售出库单");
        System.out.println("------------------------开始查询没推送金蝶的汇总销售出库单");
        List<OmsSumsaleissuebill> listInfo=  sumSaleissuebillService.selectPushEASSumSaleissuebillList(1);
        //循环找出对应没推送过EAS的汇总销售出库单
        for(int i = 0;i<listInfo.size();i++){
            OmsSumsaleissuebill pushEasBill = listInfo.get(i);
            JSONObject requestDataJSON = JSONObject.parseObject(JSONObject.toJSONString(pushEasBill,SerializerFeature.WriteDateUseDateFormat));
            requestDataJSON.put("detail",requestDataJSON.getJSONArray("omsSumsaleissuebillentryList"));
            requestDataJSON.remove("omsSumsaleissuebillentryList");
            requestDataJSON.put("sourceSystem","结算中心");//源系统标识
            //修改分录信息
            JSONArray entryJson = requestDataJSON.getJSONArray("detail");
            for(int j = 0;j<entryJson.size();j++) {
                entryJson.getJSONObject(j).put("entNo", j + 1);
            }
            JSONObject pushJSON = new JSONObject(); //组装默认推送值
            JSONObject serverJSON = new JSONObject(); //组装对应单据类型
            serverJSON.put("name","receiveSaleIssueBill");//receiveSaleIssueBill金蝶汇总销售出库单标识
            pushJSON.put("service",serverJSON);
            pushJSON.put("requestData",requestDataJSON);
            String callJSON = pushJSON.toJSONString();  //推送金蝶单据参数信息
            logger.info("------------------------推送金蝶汇总销售出库单入参case"+callJSON);
            System.out.println("------------------------推送金蝶汇总销售出库单入参case"+callJSON);
            String easrs = toPushEasWebservice(callJSON,"OMS-EAS001");
            //需要修改的汇总销售出库单
            OmsSumsaleissuebill updateBill = new OmsSumsaleissuebill();
            updateBill.setId(pushEasBill.getId());
            updateBill.setTranstime(new Date());
            if(easrs == null){
                updateBill.setIstrans("1");
                updateBill.setReason("推送金蝶成功");
            }else{
                if (easrs.contains("已存在不允许重复推送") || easrs.contains("请检查入参物料是否在金蝶配置了不需要实际出入库")){
                    updateBill.setIstrans("1");
                    updateBill.setReason(easrs);
                }else {
                    updateBill.setIstrans("0");
                    updateBill.setReason("推送金蝶失败，失败信息" + easrs);
                }
            }
            sumSaleissuebillService.updateOmsSumsaleissuebill(updateBill);
            logger.info("------------------------推送金蝶汇总销售出库单入参case"+easrs);
            System.out.println("------------------------推送金蝶汇总销售出库单入参end"+easrs);
        }
    }

    /**
     * 调用金蝶供应链单据保存接口封装方法
     * @param callJson  需要保存的数据
     * @return
     */
    public String toPushEasWebservice(String callJson,String jkNumber){
        WfzWebservice wfzWebservice = new WfzWebservice();
        wfzWebservice.setJknumber(jkNumber);
        List<WfzWebservice> wfzWebservices = wfzWebserviceService.selectWfzWebserviceList(wfzWebservice);
        wfzWebservice= wfzWebservices.get(0);
        //获取金蝶接口连接配置且登录
        String jkpassword = wfzWebservice.getJkpassword(); //密码
        String jkinurl = wfzWebservice.getJkinurl(); //业务接口
        String dcname = wfzWebservice.getDcname(); //数据中心名称
        String jkurl = wfzWebservice.getJkurl(); //登录接口
        String jkusername = wfzWebservice.getJkusername(); //接口用户
        try {
            String login = WebServiceHelper.login(jkusername, jkpassword, jkurl, dcname);   //登录EAS信息返回登录sessionID
            if(login == null){
                return "登录金蝶接口失败请查看EAS接口配置，编码为："+jkNumber;
            }
        } catch (Exception e) {
            return "登录金蝶接口异常,异常信息："+e.getMessage();
        }
        //开始调用业务接口
        String ywrs = null;
        try {
            String jkmethod = wfzWebservice.getJkmethod(); //接口方法
            Service s=new Service();
            Call call=(Call)s.createCall();
            call.setTargetEndpointAddress(jkinurl);//facade发布路径
            call.setReturnType(new QName("urn:lang.java","String"));
            call.setOperationName(jkmethod);//方法名
            call.addParameter( "requestMessage", XMLType.XSD_ANYTYPE, ParameterMode.IN);
            call.setTimeout(Integer.valueOf(1000*600));
            ywrs = (String)call.invoke(new Object[]{callJson} ); //开始调用业务接口
            JSONObject rsJSON = JSONObject.parseObject(ywrs).getJSONObject("response");
            String flag = rsJSON.getString("flag") == null ? rsJSON.getString("falg") : rsJSON.getString("flag"); //返回错误标识 success:成功，failure：失败
            String message = rsJSON.getString("message"); //返回错误信息
            if(flag.equals("failure")){
                return "调用金蝶业务接口错误返回信息："+message;
            }
        } catch (Exception e) {
            return "调用金蝶业务接口异常返回信息："+ywrs;
        }
        return null;
    }

    /**
     * 旺店通退货对账数据查询新增至金蝶
     */
    public synchronized void selWdtAfterSale(String shopNo){
        logger.info("------------------------开始查询旺店通退货对账单");
        System.out.println("------------------------开始查询旺店通退货对账单");
        String qmConfig = sysConfigService.selectConfigByKey("QM_config");
        JSONObject qmConfigJson = JSONObject.parseObject(qmConfig);
        String qmServerUrl = (String) qmConfigJson.get("QM_serverUrl");
        String qmAppKey = (String) qmConfigJson.get("QM_appKey");
        String qmAppSecret = (String) qmConfigJson.get("QM_appSecret");
        String qmTargetAppKey = (String) qmConfigJson.get("QM_targetAppKey");
        String qmSId = (String) qmConfigJson.get("QM_sId");
        DefaultQimenCloudClient client = new DefaultQimenCloudClient(qmServerUrl, qmAppKey, qmAppSecret);
        WdtAftersaleReturnCollectQueryRequest req = new WdtAftersaleReturnCollectQueryRequest();
        req.setTargetAppKey(qmTargetAppKey);
        req.setSid(qmSId);
        //对账单配置
        if (!StringUtils.isEmpty(shopNo)) req.setShopNo(shopNo);    //商铺编码
        req.setPageNo("1");
        req.setPageSize("100");
        JSONObject dataJson = new JSONObject(); //推送金蝶入参JSON
        JSONObject serviceJson = new JSONObject(); //推送金蝶入参JSON
        serviceJson.put("name","selWdtAfterSale");
        dataJson.put("service",serviceJson);
        JSONArray reqJsonArr = new JSONArray(); //具体业务数据集合
        //开始抓取旺店通数据处理推送金蝶
        try {
            WdtAftersaleReturnCollectQueryResponse response = client.execute(req);
            if ("".equals(response.getErrorCode())){
                int totalCount = Integer.parseInt(response.getTotalCount());
                //100是一页的最大数量
                int b = 100;
                int i = totalCount / b + (totalCount % b == 0 ? 0 : 1);
                for (int j = 0; j < i; j++) {
                    req = new WdtAftersaleReturnCollectQueryRequest();
                    req.setTargetAppKey(qmTargetAppKey);
                    req.setSid(qmSId);
                    if (!StringUtils.isEmpty(shopNo)) req.setShopNo(shopNo);    //商铺编码
                    req.setPageNo(Integer.toString(j+1));
                    req.setPageSize("100");
                    response = client.execute(req);
                    String retBody = response.getBody();
                    JSONObject retBodyJson = JSONObject.parseObject(retBody);
                    JSONArray orderList = retBodyJson.getJSONObject("response").getJSONArray("order_list");
                    for(int k = 0;k<orderList.size();k++){
                        reqJsonArr.add(orderList.get(k));
                    }
                }
            } else {
                throw new RuntimeException(response.getMessage());
            }
        } catch (ApiException e) {
            throw new RuntimeException("抓取处理旺店通红字对账单数据异常，错误信息："+e.getMessage());
        }
        if(reqJsonArr == null || reqJsonArr.size() == 0) throw new RuntimeException("未抓取到附和条件的旺店通蓝字对账单数据！！");
        dataJson.put("requestData",reqJsonArr); //装载具体集合业务数据
        String easrs = toPushEasWebservice(dataJson.toJSONString(),"WDT-EAS001");
        if(!StringUtils.isEmpty(easrs)){
            throw new RuntimeException(easrs);
        }
    }

    /**
     * 旺店通发货对账数据查询新增至金蝶
     */
    public synchronized void selWdtOnSale(String shopNo){
        logger.info("------------------------开始查询旺店通发货对账单");
        System.out.println("------------------------开始查询旺店通发货对账单");
        String qmConfig = sysConfigService.selectConfigByKey("QM_config");
        JSONObject qmConfigJson = JSONObject.parseObject(qmConfig);
        String qmServerUrl = (String) qmConfigJson.get("QM_serverUrl");
        String qmAppKey = (String) qmConfigJson.get("QM_appKey");
        String qmAppSecret = (String) qmConfigJson.get("QM_appSecret");
        String qmTargetAppKey = (String) qmConfigJson.get("QM_targetAppKey");
        String qmSId = (String) qmConfigJson.get("QM_sId");
        DefaultQimenCloudClient client = new DefaultQimenCloudClient(qmServerUrl, qmAppKey, qmAppSecret);
        WdtOnsaleCollectQueryRequest req = new WdtOnsaleCollectQueryRequest();
        req.setTargetAppKey(qmTargetAppKey);
        req.setSid(qmSId);
        //对账单配置
        if (!StringUtils.isEmpty(shopNo)) req.setShopNo(shopNo);    //商铺编码
        req.setPageNo("1");
        req.setPageSize("100");
        JSONObject dataJson = new JSONObject(); //推送金蝶入参JSON
        JSONObject serviceJson = new JSONObject(); //推送金蝶入参JSON
        serviceJson.put("name","selWdtOnSale");
        dataJson.put("service",serviceJson);
        JSONArray reqJsonArr = new JSONArray(); //具体业务数据集合
        //开始抓取旺店通数据处理推送金蝶
        try {
            WdtOnsaleCollectQueryResponse response = client.execute(req);
            if ("".equals(response.getErrorCode())){
                int totalCount = Integer.parseInt(response.getTotalCount());
                //100是一页的最大数量
                int b = 100;
                int i = totalCount / b + (totalCount % b == 0 ? 0 : 1);
                for (int j = 0; j < i; j++) {
                    req = new WdtOnsaleCollectQueryRequest();
                    req.setTargetAppKey(qmTargetAppKey);
                    req.setSid(qmSId);
                    if (!StringUtils.isEmpty(shopNo)) req.setShopNo(shopNo);    //商铺编码
                    req.setPageNo(Integer.toString(j+1));
                    req.setPageSize("100");
                    response = client.execute(req);
                    String retBody = response.getBody();
                    JSONObject retBodyJson = JSONObject.parseObject(retBody);
                    JSONArray orderList = retBodyJson.getJSONObject("response").getJSONArray("order_list");
                    for(int k = 0;k<orderList.size();k++){
                        reqJsonArr.add(orderList.get(k));
                    }
                }
            } else {
                throw new RuntimeException(response.getMessage());
            }
        } catch (ApiException e) {
            throw new RuntimeException("抓取处理旺店通蓝字对账单数据异常，错误信息："+e.getMessage());
        }
        if(reqJsonArr == null || reqJsonArr.size() == 0) throw new RuntimeException("未抓取到附和条件的旺店通蓝字对账单数据！！");
        dataJson.put("requestData",reqJsonArr); //装载具体集合业务数据
        String easrs = toPushEasWebservice(dataJson.toJSONString(),"WDT-EAS001");
        if(!StringUtils.isEmpty(easrs)){
            throw new RuntimeException(easrs);
        }
    }
}
